"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : show variables
Case Name   : 修改参数值后进行查询 gs_guc
Create At   : 2022/09/20
@zou_jialiang0501120110
Description :
    1、create database
    2、查询参数query_dop
    3、设置参数query_dop=2
    4、查询参数query_dop
    5、清理环境
Expect      :
    1、创建成功
    2、查询成功
    3、设置成功
    4、查询成功
    5、清理环境成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class VariablesCase(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.commonsh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.constant = Constant()
        self.guc_name = "query_dop"
        self.db_name = "db_var_0017"
        self.initial_query_dop = self.common.show_param(f"{self.guc_name}")

    def test_variables(self):
        step1_text = "-----step1:create database;expect:创建成功-----"
        self.logger.info(step1_text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
                    create database {self.db_name} dbcompatibility = 'B';'''
        self.logger.info(sql_cmd1)
        sql_res1 = self.commonsh.execut_db_sql(sql_cmd1)
        self.logger.info(sql_res1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res1,
                      "创建数据库失败:" + step1_text)

        step2_text = "-----step2:查询参数query_dop;expect:查询成功-----"
        self.logger.info(step2_text)
        sql_cmd2 = f"show variables " \
                   f"where variable_name like '{self.guc_name}';"
        self.logger.info(sql_cmd2)
        sql_res2 = self.commonsh.execut_db_sql(sql_cmd2,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res2)
        self.common.equal_sql_mdg(
            sql_res2, "Variable_name | Value ", " query_dop     | 1",
            "(1 row)", flag="1")

        step3_text = "-----step3:设置参数query_dop=2;expect:设置成功-----"
        self.logger.info(step3_text)
        guc_cmd = self.commonsh.execute_gsguc("set",
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f"{self.guc_name} = 2")
        self.logger.info(guc_cmd)
        self.assertTrue(guc_cmd, "设置参数失败:" + step2_text)

        self.logger.info("-----restart database;expect:restart success-----")
        gs_cmd = self.commonsh.restart_db_cluster()
        self.logger.info(gs_cmd)
        self.logger.info("-----check database status-----")
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

        step4_text = "-----step4:查询参数query_dop;expect:查询成功-----"
        self.logger.info(step4_text)
        sql_cmd3 = f"show variables " \
                   f"where variable_name like '{self.guc_name}';"
        self.logger.info(sql_cmd3)
        sql_res3 = self.commonsh.execut_db_sql(sql_cmd3,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res3)
        self.common.equal_sql_mdg(
            sql_res3, "Variable_name | Value ", f" {self.guc_name}     | 2",
            "(1 row)", flag="1")
        sql_cmd4 = f"show session variables " \
                   f"where variable_name like '{self.guc_name}';"
        self.logger.info(sql_cmd4)
        sql_res4 = self.commonsh.execut_db_sql(sql_cmd4,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res4)
        self.common.equal_sql_mdg(
            sql_res4, "Variable_name | Value ", f" {self.guc_name}     | 2",
            "(1 row)", flag="1")
        sql_cmd5 = f"show global variables " \
                   f"where variable_name like '{self.guc_name}';"
        self.logger.info(sql_cmd5)
        sql_res5 = self.commonsh.execut_db_sql(sql_cmd5,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res5)
        self.common.equal_sql_mdg(
            sql_res5, "Variable_name | Value ", f" {self.guc_name}     | 2",
            "(1 row)", flag="1")

    def tearDown(self):
        step5_text = "-----step5:清理环境;expect:成功-----"
        self.logger.info(step5_text)

        reset_text = "-----reset query_dop-----"
        self.logger.info(reset_text)
        re_cmd = self.commonsh.execute_gsguc("set",
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.guc_name} = "
                                             f"{self.initial_query_dop}")
        self.logger.info(re_cmd)

        restart_text = "-----restart database-----"
        self.logger.info(restart_text)
        restart_cmd = self.commonsh.restart_db_cluster()
        self.logger.info(restart_cmd)
        self.logger.info("-----check database status-----")
        status_res = self.commonsh.get_db_cluster_status()

        self.assertTrue(re_cmd, "恢复参数失败:" + reset_text)
        self.assertTrue("Degraded" in status_res or "Normal" in status_res)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
